package com.gmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gmu.domain.LoginAdminDetails;
import com.gmu.mapper.TblAdminMapper;
import com.gmu.mapper.TblMenuMapper;
import com.gmu.pojo.TblAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 木鱼~
 * @version 1.0
 */
@Service
public class AdminDetailServiceImpl extends UserDetailsServiceImpl {

    @Autowired
    private TblAdminMapper tblAdminMapper;

    @Autowired
    private TblMenuMapper tblMenuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户
        LambdaQueryWrapper<TblAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TblAdmin::getUsername,username);
        TblAdmin tblAdmin = tblAdminMapper.selectOne(wrapper);
        if (Objects.isNull(tblAdmin)){
            return null;
        }
        List<String> perms = tblMenuMapper.getPermsByAdminId(tblAdmin.getId());
        return new LoginAdminDetails(tblAdmin,perms);
    }

}
